package com.mini.gateway.web.controller;

import com.mini.gateway.core.modules.route.dto.RouteView;
import com.mini.gateway.core.modules.route.service.RouteService;
import com.mini.gateway.core.modules.server.dto.ServerRelation;
import com.mini.gateway.core.modules.server.service.ServerRelationService;
import com.mini.gateway.web.domain.LayTableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * ServerController
 *
 */
@Controller
@RequestMapping("/gateway/server")
public class ServerController {

    @Autowired
    private RouteService routeService;
    @Autowired
    private ServerRelationService serverRelationService;

    @GetMapping("/page/list")
    public Mono<String> toListPage(final Model model) {
        List<RouteView> routeList = routeService.getRouteList();
        model.addAttribute("routeList", routeList);
        return Mono.create(monoSink -> monoSink.success("server/relation_list"));
    }

    @GetMapping(value = "/data/list")
    @ResponseBody
    public Mono<LayTableResult<RouteView>> listData(String routeId) {
        List<ServerRelation> resultList = serverRelationService.getList(routeId);
        return Mono.just(LayTableResult.success(resultList));
    }
}
